using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Data; 
using System.Data.SqlClient; 
using System.Data.SqlTypes; 
using SolucionUtiles; 
using SolucionEntidad; 
namespace SolucionDatos 
{ 
   public class PersonaNaturalDao 
    { 
        private SqlConnection cn; 
        public PersonaNaturalDao(SqlConnection cn) 
        { 
           this.cn = cn; 
        } 
        public void insert(PersonaNatural obj)
        { 
           SqlCommand command = new SqlCommand("INSERT INTO persona_natural " + 
           "(idPersona,apePater,apeMater,fechaNacimiento,tipoDocumento,numDocumento) VALUES" + 
           "(@idPersona,@apePater,@apeMater,@fechaNacimiento,@tipoDocumento,@numDocumento)", this.cn); 

command.Parameters.Add(new SqlParameter("@idPersona", obj.idPersona)); 
command.Parameters.Add(new SqlParameter("@apePater", obj.apePater)); 
command.Parameters.Add(new SqlParameter("@apeMater", obj.apeMater)); 
command.Parameters.Add(new SqlParameter("@fechaNacimiento", obj.fechaNacimiento)); 
command.Parameters.Add(new SqlParameter("@tipoDocumento", obj.tipoDocumento)); 
command.Parameters.Add(new SqlParameter("@numDocumento", obj.numDocumento)); 
command.ExecuteNonQuery(); 
} 
  		public void update(PersonaNatural obj) 
    	{ 
        	SqlCommand command = new SqlCommand("UPDATE PersonaNatural " + 
			" SET " + 
"apePater=@apePater" + 
",apeMater=@apeMater" + 
",fechaNacimiento=@fechaNacimiento" + 
",tipoDocumento=@tipoDocumento" + 
",numDocumento=@numDocumento" + 
 " WHERE " + 
"idPersona=@idPersona" 
,this.cn); 
command.Parameters.Add(new SqlParameter("@idPersona", obj.idPersona)); 
command.Parameters.Add(new SqlParameter("@apePater", obj.apePater)); 
command.Parameters.Add(new SqlParameter("@apeMater", obj.apeMater)); 
command.Parameters.Add(new SqlParameter("@fechaNacimiento", obj.fechaNacimiento)); 
command.Parameters.Add(new SqlParameter("@tipoDocumento", obj.tipoDocumento)); 
command.Parameters.Add(new SqlParameter("@numDocumento", obj.numDocumento)); 
command.ExecuteNonQuery(); 
} 
       public void delete(PersonaNatural obj){ 
SqlCommand command = new SqlCommand("DELETE FROM persona_natural WHERE "  
 +"idPersona=@idPersona" 
, this.cn); 
command.Parameters.Add(new SqlParameter("@idPersona", obj.idPersona)); 
command.ExecuteNonQuery(); 
} 
        public PersonaNatural get(PersonaNatural obj) 
{ 
   PersonaNatural record = null; 
    SqlCommand command = new SqlCommand("SELECT idPersona,apePater,apeMater,fechaNacimiento,tipoDocumento,numDocumento FROM persona_natural where " 
 +"idPersona=@idPersona" 
, this.cn); 
command.Parameters.Add(new SqlParameter("@idPersona", obj.idPersona)); 

    SqlDataReader dr = command.ExecuteReader();
dr.Read(); 
record = new PersonaNatural(); 
if (dr["idPersona"] == DBNull.Value) { record.idPersona = SqlInt32.Null; } else { record.idPersona = (Int32)dr["idPersona"]; } 
if (dr["apePater"] == DBNull.Value) { record.apePater = SqlString.Null; } else { record.apePater = (String)dr["apePater"]; } 
if (dr["apeMater"] == DBNull.Value) { record.apeMater = SqlString.Null; } else { record.apeMater = (String)dr["apeMater"]; } 
if (dr["fechaNacimiento"] == DBNull.Value) { record.fechaNacimiento = SqlDateTime.Null; } else { record.fechaNacimiento = (DateTime)dr["fechaNacimiento"]; } 
if (dr["tipoDocumento"] == DBNull.Value) { record.tipoDocumento = SqlString.Null; } else { record.tipoDocumento = (String)dr["tipoDocumento"]; } 
if (dr["numDocumento"] == DBNull.Value) { record.numDocumento = SqlString.Null; } else { record.numDocumento = (String)dr["numDocumento"]; } 
this.cn.Close(); 
return record; 
} 
     public List<PersonaNatural> getList() 
{ 
   List<PersonaNatural> list = new List<PersonaNatural>(); 
    SqlCommand command = new SqlCommand("SELECT idPersona,apePater,apeMater,fechaNacimiento,tipoDocumento,numDocumento FROM persona_natural", this.cn); 
    SqlDataReader dr = command.ExecuteReader(); 
    while (dr.Read()) 
    { 
PersonaNatural obj = new PersonaNatural(); 
if (dr["idPersona"] == DBNull.Value) { obj.idPersona = SqlInt32.Null; } else { obj.idPersona = (Int32)dr["idPersona"]; } 
if (dr["apePater"] == DBNull.Value) { obj.apePater = SqlString.Null; } else { obj.apePater = (String)dr["apePater"]; } 
if (dr["apeMater"] == DBNull.Value) { obj.apeMater = SqlString.Null; } else { obj.apeMater = (String)dr["apeMater"]; } 
if (dr["fechaNacimiento"] == DBNull.Value) { obj.fechaNacimiento = SqlDateTime.Null; } else { obj.fechaNacimiento = (DateTime)dr["fechaNacimiento"]; } 
if (dr["tipoDocumento"] == DBNull.Value) { obj.tipoDocumento = SqlString.Null; } else { obj.tipoDocumento = (String)dr["tipoDocumento"]; } 
if (dr["numDocumento"] == DBNull.Value) { obj.numDocumento = SqlString.Null; } else { obj.numDocumento = (String)dr["numDocumento"]; } 
list.Add(obj); 
} 
return list; 
} 
} 
} 
